package com.itheima.service.impl.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.DishDao;
import com.itheima.dao.EmployeeDao;
import com.itheima.dao.SetmealDao;
import com.itheima.domain.*;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.NoDataException;
import com.itheima.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealDao SetmealDao;
    @Override
    //填充dto 新增;
    public SetmealDto fillSetmeal(SetmealDto setmealDto) {
        String categoryId = setmealDto.getCategoryId();
        Category cag = categoryDao.selectById(categoryId);
        //填充字段
        setmealDto.setCategoryName(cag.getName());
        //将套餐存入表中
        this.save(setmealDto);
        String setmealId = setmealDto.getId();
        //套餐--菜品关联
        List<SetmealDish> flavors = setmealDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(flavors);
        return setmealDto;
    }

    @Override
    public SetmealDto getSetmealDto(String id ) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = this.getOne(queryWrapper);
        if(setmeal==null){
            throw new NoDataException();
        }
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> flavors = setmealDishService.list(queryWrapper1);

        if(flavors==null){
            throw new NoDataException();
        }
        System.out.println("-----------------------------------");
        System.out.println(flavors);
        System.out.println("-----------------------------------");
        //添加最后一个字段
        LambdaQueryWrapper<Category> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Category::getId,setmeal.getCategoryId());
        Category category = categoryService.getOne(queryWrapper2);
        String categoryName = category.getName();


        //将查找到的数据封装到dto里.
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setCategoryName(categoryName);
        BeanUtils.copyProperties(flavors,setmealDto.getFlavors());

        return setmealDto;
    }

    @Override
    public boolean update(SetmealDto setmealDto) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getName,setmealDto.getName());
        Setmeal setmeal = this.getOne(queryWrapper);
        if(setmeal==null){
            log.info("待改数据不存在" );
            throw new NoDataException();
        }

        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        //先删除 套餐--菜品 表
        queryWrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishService.remove(queryWrapper1);
        //删除套餐
        this.remove(queryWrapper);

        String categoryId = setmealDto.getCategoryId();
        Category cag = categoryDao.selectById(categoryId);
        //填充字段

        setmealDto.setCategoryName(cag.getName());
        save(setmealDto);

        //添加当前提交过来的口味数据---
        List<SetmealDish> flavors = setmealDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId((setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(flavors);

        return true;


    }

    //删除
    @Override
    public boolean delete(String ids) {
        System.out.println("132323");
        //切割传进来的id 并遍历删除
        String[] strings = ids.split(",");
        log.info("{}",strings);
        for (int i = 0; i < strings.length; i++) {
//            String id =strings[i];
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId,strings[i]);
            Setmeal setmeal = this.getOne(queryWrapper);
            if(setmeal==null){
                log.info("要删除的数据不存在");
                throw new NoDataException();
            }
            //同时删除和菜品表关联字段
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SetmealDish::getSetmealId,strings[i]);
            List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper1);
            if(setmealDishList==null){
                log.info("要删除的数据不存在");
                throw new NoDataException();
            }
            setmealDishService.remove(queryWrapper1);
            this.remove(queryWrapper);
        }
        return true;
    }

    @Override
    public boolean updateState(String ids) {
        //切割并修改
        String[] strings = ids.split(",");
        for (int i = 0; i < strings.length; i++) {
//            String id =strings[i];
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId,strings[i]);
            Setmeal setmeal = this.getOne(queryWrapper);
            //修改state字段设置
            String status = setmeal.getStatus();
            if(status.equals("0")){
                setmeal.setId("1");
            }else if (status.equals("1")){
                setmeal.setId("0");
            }else {
                log.info("非法字段 未知数据异常");
                throw  new NoDataException();
            }
        }
        return true;
    }

    //分页查询
    @Override
    public List<SetmealDto> selectPage(Integer page, Integer pageSize, String name) {
        Page page1 = new Page<>(page,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        //判断字段是否带有name值
        if(StringUtils.hasText(name)){
            wrapper.eq("name",name);
        }
        page1=SetmealDao.selectPage(page1,wrapper);
        //取出查询的集合列表
        List<Setmeal> setmealList = page1.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();

//        Set<String> categoryIds =new HashSet<>();

        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            //复制对象
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDtos.add(setmealDto);
            //由于建表原因,如果需要修改字段,则需引入下面这段,从id再获取到名字,最后再存入
            //口味表,这里就不添加,如果有需要再回来做修改
//            String categoryId = dishDto.getCategoryId();
//            categoryIds.add(categoryId);
        }
        page1.setRecords(setmealDtos);
        return  setmealDtos;
    }

}
